package com.mhs.tracuu;

import java.util.ArrayList;

import com.mhs.mhs_gui.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter 
{

	private Context context;
	private ArrayList<Category> groups; // an array list of categories
	private ArrayList<ArrayList<Item>> children; // an array list of items

	public MyExpandableListAdapter(Context context){
		this.context = context;
	}

	public MyExpandableListAdapter(Context context, ArrayList<Category> groups,
			ArrayList<ArrayList<Item>> children) {
		this.context = context;
		this.groups = groups;
		this.children = children;
	}

	/**
	 * provide a method to add an item to a category dynamically, check if the
	 * category is existed otherwise throws an exception
	 *
	 * @param item
	 * @return void
	 */
	public void addItem(Item item) throws ClassNotFoundException {
		// check if the category is existed
		if (!this.findCategory(groups, item.getCateId())) {
			throw new ClassNotFoundException("Category doesn’t exist.");
		} else {
			Category category = this.getCateInListById(groups, item.getCateId());
			int index = groups.indexOf(category);
			/*
			 * check if children list already contained the list to hold this
			 * group or else we have to create it
			 */
			if (children.size() < (index + 1)) {
				children.add(new ArrayList<Item>());
			}
			children.get(index).add(item);
		}
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}


	public long getChildId(int groupPosition, int childPosition) {
		Item item = (Item) children.get(groupPosition).get(childPosition);
		return item.getItemId();
	}


	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Item item = (Item) getChild(groupPosition, childPosition);
		Category category = (Category) getGroup(groupPosition);

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandablelistview_child, null);
		}

		// set title for the item
		TextView tv = (TextView) convertView.findViewById(R.id.child_title);
		tv.setText("" + item.getName());

		// set icon for the item, all item in the same category have the same
		// icon
		ImageView imv = (ImageView) convertView.findViewById(R.id.child_icon);
		if ((category.getIcon() != null)
				&& (category.getIcon().trim().length() > 0)) {
			int iconUri = context.getResources().getIdentifier(
					category.getIcon(), null, context.getPackageName());
			Drawable drawable = context.getResources().getDrawable(iconUri);
			imv.setBackgroundDrawable(drawable);
		}
		return convertView;
	}


	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}


	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}


	public int getGroupCount() {
		return groups.size();
	}


	public long getGroupId(int groupPostion) {
		return groups.get(groupPostion).getId();
	}


	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		Category group = (Category) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandablelistview, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.group_title);
		tv.setText("" + group.getName());
		return convertView;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	/**
	 * search for a category through out an array list rely on its id
	 *
	 * @param arraylist
	 * @param cateId
	 * @return true if found the category otherwise false
	 */
	private boolean findCategory(ArrayList<Category> arraylist, long cateId) {
		for (Category category : arraylist) {
			if (cateId == category.getId()) {
				return true;
			}
		}
		return false;
	}
	/**
	 * method to get a category from a group depends on its id
	 * @param arraylist
	 * @param cateId
	 * @return category with the specific id if it exists otherwise return null
	 */

	private Category getCateInListById(ArrayList<Category> arraylist, long cateId){
		for (Category category : arraylist) {
			if (cateId == category.getId()) {
				return category;
			}
		}
		return null;
	}
}